#include "FreeRTOS.h"
#include "task.h"
#include "LED.h"
#include "TIM.h"
#include "semphr.h"
#include "queue.h"
#include "event_groups.h"
#include "ADC_Collect.h"
#include "MyUSART.h"
#include "ESP01.h"
#include "Connect_ONENET.h"
#include "disp.h"
#include "ui.h"

TaskHandle_t LEDTask_Handler = NULL;
TaskHandle_t TIMTask_Handle  = NULL;
TaskHandle_t TIMRead_Handle  = NULL;
TaskHandle_t lEDSUSRE_Handle  = NULL;
TaskHandle_t B5K_Handle  = NULL;
TaskHandle_t ADCLED_Handle  = NULL;
TaskHandle_t esp_Handle  = NULL;
TaskHandle_t espsend_Handle  = NULL;
TaskHandle_t mqttReC_Handle  = NULL;
TaskHandle_t u8g2_initHandel  = NULL;
TaskHandle_t u8g2_show_Handle  = NULL;
TaskHandle_t show_one_Handle  = NULL;
TaskHandle_t show_two_Handle = NULL;
SemaphoreHandle_t Mutex_Handle = NULL;
EventGroupHandle_t EventGroup_Handle = NULL;
EventGroupHandle_t ESP_EventGroup_Handle = NULL;
EventGroupHandle_t Reconnect_EventGroup_Handle = NULL;
EventGroupHandle_t WIFI_connected_Handle = NULL;
EventGroupHandle_t TimeRefresh_Handle = NULL;
QueueHandle_t myqueue_Handle = NULL;


void RTOS_Init(void)
{
	taskENTER_CRITICAL();           //进入临界区

	Mutex_Handle = xSemaphoreCreateMutex();//创建互斥信号量来用互斥使用OLED
	xSemaphoreGive(Mutex_Handle);

	EventGroup_Handle = xEventGroupCreate();
	ESP_EventGroup_Handle = xEventGroupCreate();
	Reconnect_EventGroup_Handle = xEventGroupCreate();
	WIFI_connected_Handle = xEventGroupCreate();
	TimeRefresh_Handle	= xEventGroupCreate();
	myqueue_Handle = xQueueCreate(3, sizeof(uint16_t));

	//创建LED任务
	xTaskCreate((TaskFunction_t )LED_Task,     	
							(const char*    )"LED_task",   	
							(uint16_t       )128, 
							(void*          )NULL,				
							(UBaseType_t    )2,	
							(TaskHandle_t*  )&LEDTask_Handler); 
	xTaskCreate((TaskFunction_t )TIM_Task,     	
							(const char*    )"TIM_Task",   	
							(uint16_t       )128, 
							(void*          )NULL,				
							(UBaseType_t    )2,	
							(TaskHandle_t*  )&TIMTask_Handle); 					
	xTaskCreate((TaskFunction_t )LED_Suspend_Task,     	
							(const char*    )"LED_Suspend_TASK",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )2,	
							(TaskHandle_t*  )&lEDSUSRE_Handle);
	xTaskCreate((TaskFunction_t )B5K_Task,     	
							(const char*    )"B5K_Task",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )3,	
							(TaskHandle_t*  )&B5K_Handle);		
							
	xTaskCreate((TaskFunction_t )ADC_Get_LED_Task,     	
							(const char*    )"ADCGetLEDTask",   	
							(uint16_t       )512, 
							(void*          )NULL,				
							(UBaseType_t    )4,	
							(TaskHandle_t*  )&ADCLED_Handle);							
	xTaskCreate((TaskFunction_t )ESP_Init_task,     	
							(const char*    )"esptask",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )5,	
							(TaskHandle_t*  )&esp_Handle);
	xTaskCreate((TaskFunction_t )ESP_SendTask,     	
							(const char*    )"espsendtask",   	
							(uint16_t       )512, 
							(void*          )NULL,				
							(UBaseType_t    )5,	
							(TaskHandle_t*  )&espsend_Handle);	
	// xTaskCreate((TaskFunction_t )Re_connect_Task,     	
	// 						(const char*    )"Re_connect",   	
	// 						(uint16_t       )512, 
	// 						(void*          )NULL,				
	// 						(UBaseType_t    )5,	
	// 						(TaskHandle_t*  )&mqttReC_Handle);
	xTaskCreate((TaskFunction_t )u8g2_init_task,     	
							(const char*    )"u8g2task",   	
							(uint16_t       )128, 
							(void*          )NULL,				
							(UBaseType_t    )4,	
							(TaskHandle_t*  )&u8g2_initHandel);
	xTaskCreate((TaskFunction_t )ui_show_task,     	
							(const char*    )"u8g2showtask",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )2,	
							(TaskHandle_t*  )&u8g2_show_Handle);
	xTaskCreate((TaskFunction_t )show_one_Task,     	
							(const char*    )"showonetask",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )2,	
							(TaskHandle_t*  )&show_one_Handle);	
	xTaskCreate((TaskFunction_t )show_two_task,     	
							(const char*    )"showtwotask",   	
							(uint16_t       )256, 
							(void*          )NULL,				
							(UBaseType_t    )3,	
							(TaskHandle_t*  )&show_two_Handle);		
							vTaskSuspend(show_two_Handle);																								
	taskEXIT_CRITICAL();            //退出临界区	
	vTaskStartScheduler();          //开启任务调度
}

